<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 测试接口
 */
class Test extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 数据归纳-catalogue.json归入resource
     *
     */
    public function test()
    {
        $name = input('name');
        $pid = input('pid/d');
        $sp = $name.'_products';// 商品教材表
        $sc = $name.'_catalogues';// 章节表
        $sr = $name.'_resources';// 资源表
        $where = [];
        if ($pid) {
            $where['productId'] = $pid;
        }
        $spdata = Db::name($sp)->where($where)->select();
        foreach ($spdata as $key => $value) {
            $scdata = Db::name($sc)->where('productId',$value['productId'])->select();
            //$modarr =  ['新闻分享_1'=>'发音要领','单词跟读_1'=>'例词跟读','课文跟读_1'=>'例句跟读','听报写'=>'听报写'];
            foreach ($scdata as $va) {
               $va['gunzipResPath_full'] = fileurl($va['gunzipResPath'],true);
               $calalogue = json_decode(file_get_contents($va['gunzipResPath_full'].'/catalogue.json'),true);
               //halt($calalogue);
               if (is_array($calalogue)) {
                   // code...
               
               foreach ($calalogue as $vv) {
                    $moduleInfo = $vv['moduleInfo'];
                    $resourcelist = $vv['resourceList'];

                    foreach ($resourcelist as $k=>$vs) {
                        if ((isset($vv['isDeleted'])&&$vv['isDeleted']==0) || (!isset($vv['isDeleted']))) {
                            $data = [];
                            $pid = isset($vs['parentVersionId'])?$vs['parentVersionId']:0;
                            $data['parentVersionId'] = isset($vs['parentVersionId'])?$vs['parentVersionId']:0;
                            $data['versionId'] = $vs['versionId']!=0?$vs['versionId']:strval($pid).'1';
                            
                             $ss = Db::name($sr)->where('versionId',$data['versionId'])->find();
                            if (!$ss) {
                                $data['resourceName'] = $vs['resourceName'];
                                $data['resourceType'] = $vs['resourceType'];
                                $data['gunzipResPath'] = '/file/model/res/'.$vs['resourceFile'];
                                $data['productId'] = $value['productId'];
                                $data['cId'] = $moduleInfo['cId'];
                                $data['packageCatalogueId'] = $moduleInfo['cId'];
                                $data['cType'] = 1;
                                $data['moduleId'] = $moduleInfo['moduleId'];
                                $data['moduleName'] = $moduleInfo['moduleName'];//isset($modarr[$moduleInfo['moduleName']])?$modarr[$moduleInfo['moduleName']]:$moduleInfo['moduleName'];
                                $data['id'] = $k;
                                Db::name($sr)->insert($data);
                            }
                            
                        }
                        
                        // halt(111);
                    }
                    
                }
            }else{
                print($va['id']).' ';
            }
                print(1);
           }
        }
       $this->success('请求成功');
    }

    /**
     * 数据归纳-catalogue.json归入resource children
     *
     */
    public function testch()
    {
        $name = input('name');
        $pid = input('pid/d');
        $sp = $name.'_products';// 商品教材表
        $sc = $name.'_catalogues';// 章节表
        $sr = $name.'_resources';// 资源表
        $where = [];
        if ($pid) {
            $where['productId'] = $pid;
        }
        $spdata = Db::name($sp)->where($where)->select();
        foreach ($spdata as $key => $value) {
            $scdata = Db::name($sc)->where('productId',$value['productId'])->where(['children'=>['<>','[]']])->select();
            //$modarr =  ['新闻分享_1'=>'发音要领','单词跟读_1'=>'例词跟读','课文跟读_1'=>'例句跟读','听报写'=>'听报写'];
                foreach ($scdata as $va) {
                    $chdata =  json_decode(str_replace("'",'"',$va['children']),true);

                    foreach ($chdata as $chv) {
                        
                   $chv['gunzipResPath_full'] = fileurl($chv['gunzipResPath'],true);
                   $calalogue = json_decode(file_get_contents($chv['gunzipResPath_full'].'/catalogue.json'),true);
                   //halt($calalogue);
                    
                   if (is_array($calalogue)) {
                       // code...
                   
                   foreach ($calalogue as $vv) {
                        $moduleInfo = $vv['moduleInfo'];
                        $resourcelist = $vv['resourceList'];

                        foreach ($resourcelist as $k=>$vs) {
                            if ((isset($vv['isDeleted'])&&$vv['isDeleted']==0) || (!isset($vv['isDeleted']))) {
                                $data = [];
                                $pid = isset($vs['parentVersionId'])?$vs['parentVersionId']:0;
                                $data['parentVersionId'] = isset($vs['parentVersionId'])?$vs['parentVersionId']:0;
                                $data['versionId'] = $vs['versionId']!=0?$vs['versionId']:strval($pid).'1';
                                
                                 $ss = Db::name($sr)->where('versionId',$data['versionId'])->find();
                                if (!$ss) {
                                    $data['resourceName'] = $vs['resourceName'];
                                    $data['resourceType'] = $vs['resourceType'];
                                    $data['gunzipResPath'] = '/file/model/res/'.$vs['resourceFile'];
                                    $data['productId'] = $value['productId'];
                                    $data['cId'] = $moduleInfo['cId'];
                                    $data['packageCatalogueId'] = $moduleInfo['cId'];
                                    $data['cType'] = 1;
                                    $data['moduleId'] = $moduleInfo['moduleId'];
                                    $data['moduleName'] = $moduleInfo['moduleName'];//isset($modarr[$moduleInfo['moduleName']])?$modarr[$moduleInfo['moduleName']]:$moduleInfo['moduleName'];
                                    $data['id'] = $k;
                                    Db::name($sr)->insert($data);
                                }
                                
                            }
                            
                            // halt(111);
                        }
                        
                    }
                }else{
                    print($va['id']).' ';
                }
                    print(1);
               }
            }
        }
       $this->success('请求成功');
    }

    /**
     * 数据归纳-json文件链接补全
     *
     */
    public function test2()
    {
        $name = input('name');
        $sr = $name.'_resources';// 资源表
        $srr = Db::name($sr)->select();
        foreach ($srr as $vs) {
            Db::name($sr)->where('versionId',$vs['versionId'])->update(['gunzipResPath'=>$vs['gunzipResPath'].'/resource/resource.json']);
        }
        $this->success('请求成功');
    }


    /**
     * 数据归纳-有重复versionId
     *
     */
    public function test3()
    {
        $name = input('name');
        $n = input('n');
        $sr = $name.'_resources';// 资源表
        $srr = Db::name($sr)->group('versionId')->select();
        foreach ($srr as $vs) {
            $count = Db::name($sr)->where('versionId',$vs['versionId'])->count();
            if ($count>1) {

               Db::name($sr)->where(['versionId'=>$vs['versionId'],'resourceName'=>$vs['resourceName']])->update(['versionId'=>intval($vs['versionId']).$n]);
            }
            
        }
        $this->success('请求成功');
    }

    /**
     * 数据归纳-json文件链接补全
     *
     */
    public function test4()
    {
        $name = input('name');
        $sr = $name.'_resources';// 资源表
        $srr = Db::name($sr)->where(['gunzipResPath'=>['like','%model%']])->select();
        foreach ($srr as $vs) {
            $vs['gunzipResPath'] = str_replace('model','writing',$vs['gunzipResPath']);
            Db::name($sr)->where('versionId',$vs['versionId'])->update(['gunzipResPath'=>$vs['gunzipResPath']]);
        }
        $this->success('请求成功');
    }
}
